﻿using Business.Core.Annotations;
using Business.Data;
using Common;
using DataModel;
using LinqToDB;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys
{
    /// <summary>
    /// 任务字典
    /// </summary>
    public class TaskDicMerber : Common.BusinessBase
    {
        #region //任务字典管理
        [Doc("字典列表", Group = "任务字典管理")]
        public virtual async Task<dynamic> TaskDicList(int page, int limit, string title)
        {
            AuthInfo authinfo = Logins.Authorize();

            using (var con = Data.DB.GetConnection())
            {
                var list = con.MANAGER_TASK_DIC.AsQueryable();

                list = list.Where(p => p.ACTIVE == 1 && p.TYPEDIC == "task");
                if (!string.IsNullOrWhiteSpace(title)) //项目名称
                    list = list.Where(p => p.CODENAME.Contains(title));

                list = list.OrderBy(p=>new { p.TYPE,p.SORTINDEX});

                var pageData = list.GetPaging(page, limit);

                var result = new { code = 0, msg = "", count = pageData.Count, data = pageData.Data };

                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                return JsonConvert.SerializeObject(result, Formatting.Indented, timeFormat);
            }
        }

        [Doc("任务字典列表", Group = "任务字典管理")]
        public virtual async Task<dynamic> TaskDicListAll()
        {
            using (var con = Data.DB.GetConnection())
            {
                var list = con.MANAGER_TASK_DIC.AsQueryable();
                list = list.Where(p => p.ACTIVE == 1 && p.TYPEDIC == "task");
                return JsonConvert.SerializeObject(list);
            }
        }

        [Doc("新增或者修改字典", Group = "任务字典管理")]
        public virtual async Task<dynamic> AddOrUpdateTaskDic(string arg)
        {
            int flag = 0;
            AuthInfo authinfo = Logins.Authorize();
            var oModel = JsonConvert.DeserializeObject<MANAGER_TASK_DIC>(arg);
            using (var con = Data.DB.GetConnection())
            {
                oModel.CREATETIME = DateTime.Now;
                oModel.USERID = authinfo.userid;
                oModel.TYPEDIC = "task";
                oModel.ACTIVE = 1;

                if (string.IsNullOrEmpty(oModel.ID)) //新增
                {
                    oModel.CODE = GetTaskDicMaxCode(oModel.TYPE).ToString();
                    oModel.ID = Guid.NewGuid().ToString();
                    flag = await con.InsertAsync(oModel);
                }
                else
                {
                    flag = await con.UpdateAsync(oModel);
                }
                return JsonConvert.SerializeObject(flag);
            }
        }

        [Doc("逻辑删除任务字典", Group = "任务字典管理")]
        public virtual async Task<dynamic> DeleteTaskDic(string delIds)
        {
            using (var con = Data.DB.GetConnection())
            {
                var flag = 0;
                if (!String.IsNullOrEmpty(delIds))
                {
                    var idArray = delIds.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (idArray.Length > 0)
                    {
                        foreach (var item in idArray)
                        {
                            var oModel = con.MANAGER_TASK_DIC.Where(c => c.ID == item).FirstOrDefault();
                            if (oModel != null)
                            {
                                oModel.ACTIVE = 0;
                                flag += await con.UpdateAsync(oModel);
                            }
                        }
                    }
                }

                return (flag);
            }
        }

        public int GetTaskDicMaxCode(decimal? type)
        {
            int flag = 0;
            using (var con = Data.DB.GetConnection())
            {
                var oModel = con.MANAGER_TASK_DIC.Where(c => c.TYPE == type).OrderByDescending(c => c.CODE).FirstOrDefault();
                if (oModel != null)
                    flag = Convert.ToInt32(oModel.CODE) + 1;
            }
            return flag;
        }
        #endregion
    }
}
